{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'formatters'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_15388/2891831409.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mbaseanalysis\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mBaseAnalysis\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mk:\\1111111\\INCORE\\incore3.1\\pyincore-develop\\pyincore-develop\\pyincore\\bd\\baseanalysis.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;31m# TODO: exception handling for validation and set methods\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mdataservice\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mDataService\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mdataset\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mDataset\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mk:\\1111111\\INCORE\\incore3.1\\pyincore-develop\\pyincore-develop\\pyincore\\bd\\dataservice.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mntpath\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mglobals\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpyglobals\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mk:\\1111111\\INCORE\\incore3.1\\pyincore-develop\\pyincore-develop\\pyincore\\bd\\globals.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m \u001b[0mLOGGING_CONFIG\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mabspath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mabspath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdirname\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m__file__\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'logging.ini'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 35\u001b[1;33m \u001b[0mlogging_config\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfileConfig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mLOGGING_CONFIG\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     36\u001b[0m \u001b[0mLOGGER\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlogging\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetLogger\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'pyincore'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mk:\\ana\\lib\\logging\\config.py\u001b[0m in \u001b[0;36mfileConfig\u001b[1;34m(fname, defaults, disable_existing_loggers)\u001b[0m\n\u001b[0;32m     69\u001b[0m             \u001b[0mcp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     70\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 71\u001b[1;33m     \u001b[0mformatters\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_create_formatters\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     72\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     73\u001b[0m     \u001b[1;31m# critical section\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mk:\\ana\\lib\\logging\\config.py\u001b[0m in \u001b[0;36m_create_formatters\u001b[1;34m(cp)\u001b[0m\n\u001b[0;32m    102\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_create_formatters\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    103\u001b[0m     \u001b[1;34m\"\"\"Create and return formatters\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 104\u001b[1;33m     \u001b[0mflist\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcp\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"formatters\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"keys\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    105\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mflist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    106\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mk:\\ana\\lib\\configparser.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m    961\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__getitem__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    962\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mkey\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdefault_section\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhas_section\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 963\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    964\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_proxies\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    965\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'formatters'"
     ]
    }
   ],
   "source": [
    "from baseanalysis import BaseAnalysis\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bridge damage for Shelby county, TN with New Madrid earthquake."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# New Madrid earthquake using Atkinson Boore 1995\n",
    "hazard_type = \"earthquake\"\n",
    "hazard_id = \"5b902cb273c3371e1236b36b\"\n",
    "\n",
    "# Bridget inventory in Shelby county, TN\n",
    "bridge_dataset_id = \"5a284f2dc7d30d13bc082040\"\n",
    "\n",
    "# Default Bridge Fragility Mapping\n",
    "mapping_id = \"5b47bcce337d4a37755e0cb2\"\n",
    "\n",
    "# Use hazard uncertainty for computing damage\n",
    "use_hazard_uncertainty = False\n",
    "# Use liquefaction (LIQ) column of bridges to modify fragility curve\n",
    "use_liquefaction = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bridge_dmg = BridgeDamage(client)\n",
    "# Load input dataset\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", bridge_dataset_id) \n",
    "\n",
    "# Load fragility mapping\n",
    "fragility_service = FragilityService(client)\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Specify the result name\n",
    "result_name = \"bridge_result\"\n",
    "\n",
    "# Set analysis parameters\n",
    "bridge_dmg.set_parameter(\"result_name\", result_name)\n",
    "bridge_dmg.set_parameter(\"hazard_type\", hazard_type)\n",
    "bridge_dmg.set_parameter(\"hazard_id\", hazard_id)\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run bridge damage analysis\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Retrieve result dataset\n",
    "result = bridge_dmg.get_output_dataset(\"result\")\n",
    "\n",
    "# Convert dataset to Pandas DataFrame\n",
    "df = result.get_dataframe_from_csv()\n",
    "\n",
    "# Display top 5 rows of output data\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Bridge damage for Seaside, OR with an earthquake."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Load input dataset and fragility mapping\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", \"5d6ede5db9219c34b56fc20b\")\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(\"5d55c3a1b9219c0689f1f898\"))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "bridge_dmg.set_parameter(\"result_name\", \"seaside_eq_bridge_dmg\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", \"earthquake\")\n",
    "bridge_dmg.set_parameter(\"hazard_id\", \"5ba8f379ec2309043520906f\")\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read result dataset\n",
    "df = pd.read_csv(\"seaside_eq_bridge_dmg.csv\")\n",
    "\n",
    "# Display top 5 rows of output data\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### Bridge damage for Seaside, OR with a tsunami."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Load input dataset and fragility mapping\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", \"5d6ede5db9219c34b56fc20b\")\n",
    "mapping_set = MappingSet(fragility_service.get_mapping(\"5d275000b9219c3c553c7202\"))\n",
    "bridge_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "bridge_dmg.set_parameter(\"result_name\", \"seaside_tsu_bridge_dmg\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", \"tsunami\")\n",
    "bridge_dmg.set_parameter(\"hazard_id\", \"5d27b986b9219c3c55ad37d0\")\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bridge damage for Galveston, TX with Hurricane"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hazard_type = \"hurricane\"\n",
    "hazard_id = \"5f15cd627db08c2ccc4e3bab\"\n",
    "\n",
    "# Galveston bridge\n",
    "bridge_dataset_id = \"60620320be94522d1cb9f7f0\"\n",
    "\n",
    "# Galveston hurricane bridge mapping\n",
    "refactored_mapping_id = \"6062254b618178207f66226c\"\n",
    "\n",
    "# Create bridge damage\n",
    "bridge_dmg = BridgeDamage(client)\n",
    "\n",
    "# Load input datasets\n",
    "bridge_dmg.load_remote_input_dataset(\"bridges\", bridge_dataset_id)\n",
    "\n",
    "# Load fragility mapping\n",
    "fragility_service = FragilityService(client)\n",
    "refactored_mapping_set = MappingSet(fragility_service.get_mapping(refactored_mapping_id))\n",
    "bridge_dmg.set_input_dataset('dfr3_mapping_set', refactored_mapping_set)\n",
    "\n",
    "# Set analysis parameters\n",
    "bridge_dmg.set_parameter(\"fragility_key\", \"Hurricane SurgeLevel and WaveHeight Fragility ID Code\")\n",
    "bridge_dmg.set_parameter(\"result_name\", \"galveston_bridge_dmg_result\")\n",
    "bridge_dmg.set_parameter(\"hazard_type\", hazard_type)\n",
    "bridge_dmg.set_parameter(\"hazard_id\", hazard_id)\n",
    "bridge_dmg.set_parameter(\"num_cpu\", 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run bridge damage analysis\n",
    "bridge_dmg.run_analysis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = bridge_dmg.get_output_dataset(\"result\")\n",
    "df = result.get_dataframe_from_csv()\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "eccabbadd366c3c4de7f475f0f09ad3417786bae9ff2de11bcd35913807d08d4"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
